from django.db import models

from user.models import User


# Create your models here.
class Post(models.Model):
    """
    用户文章类
    """
    STATUS_NORMAL = 1
    STATUS_HIDDEN = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, "正常"),
        (STATUS_HIDDEN, "隐藏")
    )
    title = models.CharField(max_length=40, verbose_name="标题")
    desc = models.CharField(max_length=100, verbose_name="摘要")
    content = models.TextField(verbose_name="正文")
    status = models.PositiveSmallIntegerField(default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name="状态")
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="持有者", related_name="owner")
    thumbers = models.ManyToManyField(User, verbose_name="点赞者", related_name="thumbers")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    visitor_count = models.PositiveIntegerField(default=0, verbose_name="访问人数")
    comment_count = models.PositiveIntegerField(default=0, verbose_name="评论人数")
    thumb_up_count = models.PositiveIntegerField(default=0, verbose_name="点赞人数")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = verbose_name_plural = "帖子"
        ordering = ['-created_time']


class Comment(models.Model):
    """
    用户评论类
    """
    STATUS_NORMAL = 1
    STATUS_HIDDEN = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, "正常"),
        (STATUS_HIDDEN, "隐藏")
    )
    content = models.TextField(verbose_name="评论内容")
    status = models.PositiveIntegerField(default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name="状态")
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="持有者")
    post = models.ForeignKey(Post, on_delete=models.CASCADE, verbose_name="评论目标")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.post.title

    class Meta:
        verbose_name = verbose_name_plural = "评论"
        ordering = ['-created_time']
